﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CTEM.Controllers.BUS;
using CTEM.Controllers.Global;
using CTEM.Models;
using CTEM.ModuleForms.EmployeeManagement;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;

namespace CTEM.ModuleForms.ManageEmployee
{
    public partial class EmployeeExpenseReceipts : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private List<EmployeeSalaryDTO> _listSalary;
        private readonly List<EmployeeSalaryDTO> _listSalaryHistory = new List<EmployeeSalaryDTO>();
        private readonly EmployeeBUS _empBus = new EmployeeBUS();
        private List<EmployeeAdvanceDTO> _listAdvance = new List<EmployeeAdvanceDTO>();
        private  DateTime _fromDate;
        private  DateTime _toDate;
        private bool _isNew;
        private bool _isUpdate;
        private bool _isEdit;

        public EmployeeExpenseReceipts()
        {
            InitializeComponent();
            gridView1.BestFitColumns();
            _isNew = false;
            _isUpdate = false;
           
            LoadData();
        }
       

        private void LoadData()
        {
            _listSalary = _empBus.LoadSalary();
            //foreach (EmployeeSalaryDTO employeeSalary  in _listSalary)
            //{
            //    employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
            //    var year = employeeSalary.MonthFormat.Year;
            //    var month = employeeSalary.MonthFormat.Month;
            //    _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
            //    _fromDate = new DateTime(year, month, 1);
            //    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate,_toDate,"Thu Nợ Hộ");
            //    long totalAdvance = 0;
            //    foreach (EmployeeAdvanceDTO advance  in _listAdvance)
            //    {
            //        totalAdvance += advance.TotalAdvance;
            //    }
            //    employeeSalary.Debt1 = totalAdvance;
            //    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Phụ Cấp");
                
            //    foreach (EmployeeAdvanceDTO advance2 in _listAdvance)
            //    {
            //        totalAdvance += advance2.TotalAdvance;
            //    }
            //    employeeSalary.TotalAllowance = totalAdvance;
            //    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Đền Vật Tư");

            //    foreach (EmployeeAdvanceDTO advance1 in _listAdvance)
            //    {
            //        totalAdvance += advance1.TotalAdvance;
            //    }
            //    employeeSalary.Debt2 = totalAdvance;
            //    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Ứng Lương");

            //    foreach (EmployeeAdvanceDTO advance3 in _listAdvance)
            //    {
            //        totalAdvance += advance3.TotalAdvance;
            //    }
            //    employeeSalary.TotalAdvanceSalary = totalAdvance;
              
            //}
            employerDTOBindingSource.DataSource = _empBus.LoadAllEmployee();
            employeeSalaryDTOBindingSource.DataSource = _listSalary;
        }

        private static bool ValidateEmployeeSalary(EmployeeSalaryDTO employeeSalary, out string output, out int i)
        {
            EmployeeBUS _empBus = new EmployeeBUS();

            if (employeeSalary.EmployeeID <= 0)
            {
                output = "bạn chua chọn tên nhân viên";
                i = 1;
                return false;
            }
            else if (employeeSalary.Month == null)
            {
                output = "bạn chưa chọn tháng lương cho bảng lương";
                i = 2;
                return false;
            }
            else if (_empBus.LoadSalaryByEmployeeIDAndMonth(employeeSalary.EmployeeID, employeeSalary.Month,employeeSalary.EmployeeSalaryID))
            {
                output = "bảng lương cho nhân viên này trong tháng này đả được tạo";
                i = 1;
                return false;
            }
            else if (employeeSalary.Salary < 0)
            {
                output = "lương nhân viên phải lớn hơn hoặc bằng 0";
                i = 3;
                return false;
            }

            output = "";
            i = 0;
            return true;
        }

        
        private static bool ValidateEmployeeSalaryBeforeCLick(EmployeeSalaryDTO employeeSalary, out string output, out int i)
        {
          
            if (employeeSalary.EmployeeID <= 0)
            {
                output = "bạn chua chọn tên nhân viên";
                i = 1;
                return false;
            }
            else if (employeeSalary.Month == null)
            {
                output = "bạn chưa chọn tháng lương cho bảng lương";
                i = 2;
                return false;
            }

            output = "";
            i = 0;
            return true;

        }

        public  EmployeeSalaryDTO ConvertValueForEmployeeSalary (EmployeeSalaryDTO employeeSalary)
        {
            employeeSalary.Month = employeeSalary.MonthFormat.ToString("yyyyMM");  
            employeeSalary.Salarypc = employeeSalary.Salary*10/100;

            employeeSalary.GenDebt = 0;

            if (_isNew)
            {
                employeeSalary.ActualIncome = employeeSalary.Salary
                                          - employeeSalary.Salarypc - employeeSalary.TotalAdvanceSalary -
                                          employeeSalary.Debt1
                                          - employeeSalary.Debt2 - employeeSalary.DebtPay;
            }
            else
            {
                
                long actualIncome = employeeSalary.Salary
                                          - employeeSalary.Salarypc - employeeSalary.TotalAdvanceSalary -
                                          employeeSalary.Debt1
                                          - employeeSalary.Debt2 - employeeSalary.DebtPay;
                long temp = employeeSalary.ActualIncome - actualIncome;
                employeeSalary.GenDebt = employeeSalary.GenDebt + temp;
            }
            if (employeeSalary.ActualIncome < 0)
            {
                employeeSalary.GenDebt += employeeSalary.ActualIncome;
                employeeSalary.ActualIncome = 0;

            }
            

            
            return employeeSalary;
        }

   

        private void btn_Delete_ItemClick(object sender, ItemClickEventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var employeeSalary = gridView1.GetRow(rowIndex) as EmployeeSalaryDTO;
            if (employeeSalary != null)
                if (MessageBox.Show(string.Format("Bạn có chắc chắn muốn xóa không?"), "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    bool success = _empBus.DeleteSalary(employeeSalary.EmployeeSalaryID);
                    if (success)
                    {
                        LoadData();
                        employeeSalary.Status = 3;
                        _listSalaryHistory.Add(employeeSalary);
                    }
                    else
                        MessageBox.Show("Không thể xóa.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            
            _isNew = true;
            gridView1.SetRowCellValue(e.RowHandle, "DateReceive", DateTime.Today);
            gridView1.SetRowCellValue(e.RowHandle, "Month", DateTime.Today);
            gridView1.SetRowCellValue(e.RowHandle, "MonthFormat", DateTime.Today);
      
           
        }

        private void EmployeeExpenseReceipts_Enter(object sender, EventArgs e)
        {
            LoadData();
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (!_isNew)
            {
                var view = sender as ColumnView;
                var i = e.RowHandle;
                var employeeSalary = gridView1.GetRow(i) as EmployeeSalaryDTO;
                employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
              
                if (employeeSalary != null)
                {
                    var oldEmployeeSalary = _empBus.LoadSalaryById(employeeSalary.EmployeeSalaryID); 
                    

                    string message;
                    int pos;
                    if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                    {
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        if (result == DialogResult.No)
                        {
                            _isUpdate = false;
                            view.ClearColumnErrors();
                            LoadData();
                            return;
                        }
                        else
                        {
                            _isUpdate = true;
                            gridView1.FocusedRowHandle = i;
                            gridView1.FocusedColumn = e.Column;
                            return;
                        }
                    }
                   
                    if (!_empBus.UpdateEmployeeSalary(employeeSalary))
                    {
                        MessageBox.Show("Không thể cập nhật thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        _isUpdate = false;
                    }
                    else
                    {
                        _isUpdate = false;
                        LoadData();
                        alertControlMain.Show(this, Constants.CONFIRM, String.Format("Bạn đả sửa bảng lương nhân viên {0} Thành Công!",employeeSalary.Fullname));
                        if (oldEmployeeSalary != null)
                        {
                            oldEmployeeSalary.Status = 2;
                            _listSalaryHistory.Add(oldEmployeeSalary);
                        }
                    }
                }
            }
        }

        private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            if (_isNew)
            {
                
                var view = sender as ColumnView;
                var employeeSalary = e.Row as EmployeeSalaryDTO;
                
                employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
               // employeeSalary.EmployeeID = 0;
                string message;
              
                int pos;

                if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                {
                    e.Valid = false;
                    if (view != null)
                    {
                        view.ClearColumnErrors();
                        view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                    }
                    DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.No)
                    {
                        _isNew = false;
                        e.Valid = true;
                        LoadData();
                    }
                }
                else
                {
                    
                    bool success = _empBus.CreateEmployeeSalary((employeeSalary));
                    if (success == false)
                    {

                        e.Valid = false;
                        DialogResult result = MessageBox.Show("Bạn xem lại kết nối tới máy chủ \nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;
                            e.Valid = true;
                            LoadData();
                        }
                    }
                    else
                    {
                        alertControlMain.Show(this, Constants.CONFIRM, String.Format("Bạn Đã tạo bảng lương nhân viên {0} Thành Công!", employeeSalary.Fullname));
                        employeeSalary.Status = 1;
                        _listSalaryHistory.Add(employeeSalary);
                        _isNew = false;
                        LoadData();
                    }
                    
                }
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
          
            if (e.FocusedRowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle)
            {
                
                gridView1.OptionsBehavior.ReadOnly = false;
                _isNew = true;

            }
            else
            {
                
                gridView1.OptionsBehavior.ReadOnly = !_isEdit;
                
            }
        }

        private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        private void btnUndo_ItemClick(object sender, ItemClickEventArgs e)
        {
           
            if (_listSalaryHistory.Count > 0)
            {
                try
                {
                    EmployeeSalaryDTO employeeSalary = _listSalaryHistory[_listSalaryHistory.Count - 1];
                    if (employeeSalary.Status == 1)
                    {
                       
                        _empBus.DeleteSalaryByEmployeeIDAndMonth(employeeSalary.EmployeeID,employeeSalary.Month);
                    }
                    else if (employeeSalary.Status == 2)
                    {
                        _empBus.UpdateEmployeeSalary(employeeSalary);
                    }
                    else if (employeeSalary.Status == 3)
                    {
                        _empBus.CreateEmployeeSalary(employeeSalary);
                    }
                }
                catch
                {
                    MessageBox.Show("Không thể hồi phục dữ liệu này");
                }
               
            }
            LoadData();
        }
        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var view = sender as ColumnView;
            var employeeSalary = gridView1.GetRow(rowIndex) as EmployeeSalaryDTO;
            if (_isNew)
            {
                if (employeeSalary != null)
                {
                    string message;
                    int pos;

                    if (!ValidateEmployeeSalaryBeforeCLick(employeeSalary, out message, out pos))
                    {

                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                    {
                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }

                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    else
                    {
                        using (var advanceManagementForm = new AdvanceManagement("Phụ Cấp", employeeSalary))
                        {
                            advanceManagementForm.ShowDialog();
                            employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                            var year = employeeSalary.MonthFormat.Year;
                            var month = employeeSalary.MonthFormat.Month;
                            _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                            _fromDate = new DateTime(year, month, 1);
                            _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Phụ Cấp");
                            long totalAdvance = 0;
                            foreach (EmployeeAdvanceDTO advance in _listAdvance)
                            {
                                totalAdvance += advance.TotalAdvance;
                            }
                            employeeSalary.TotalAllowance = totalAdvance;
                            employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                            _empBus.UpdateEmployeeSalary(employeeSalary);
                           
                        }

                    }
                }
            }
            else
            {
                using (var advanceManagementForm = new AdvanceManagement("Phụ Cấp", employeeSalary))
                {
                    advanceManagementForm.ShowDialog();
                    employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                    var year = employeeSalary.MonthFormat.Year;
                    var month = employeeSalary.MonthFormat.Month;
                    _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                    _fromDate = new DateTime(year, month, 1);
                    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Phụ Cấp");
                    long totalAdvance = 0;
                    foreach (EmployeeAdvanceDTO advance in _listAdvance)
                    {
                        totalAdvance += advance.TotalAdvance;
                    }
                    employeeSalary.TotalAllowance = totalAdvance;
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    _empBus.UpdateEmployeeSalary(employeeSalary);
                    
                }
            }


        }

        private void repositoryItemButtonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var view = sender as ColumnView;
            var employeeSalary = gridView1.GetRow(rowIndex) as EmployeeSalaryDTO;
            if (_isNew)
            {
                if (employeeSalary != null)
                {
                    string message;
                    int pos;

                    if (!ValidateEmployeeSalaryBeforeCLick(employeeSalary, out message, out pos))
                    {

                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                       
                        }
                    }
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                    {
                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }

                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                        }
                    }
                    else
                    {
                        using (var advanceManagementForm = new AdvanceManagement("Ứng Lương", employeeSalary))
                        {
                            advanceManagementForm.ShowDialog();

                            employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                            var year = employeeSalary.MonthFormat.Year;
                            var month = employeeSalary.MonthFormat.Month;
                            _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                            _fromDate = new DateTime(year, month, 1);
                            _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Ứng Lương");
                            long totalAdvance = 0;
                            foreach (EmployeeAdvanceDTO advance in _listAdvance)
                            {
                                totalAdvance += advance.TotalAdvance;
                            }
                            employeeSalary.TotalAdvanceSalary = totalAdvance;
                            employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                            _empBus.UpdateEmployeeSalary(employeeSalary);
                          ;
                        }

                    }
                }
            }
            else
            {
                using (var advanceManagementForm = new AdvanceManagement("Ứng Lương", employeeSalary))
                {
                    advanceManagementForm.ShowDialog();

                    employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                    var year = employeeSalary.MonthFormat.Year;
                    var month = employeeSalary.MonthFormat.Month;
                    _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                    _fromDate = new DateTime(year, month, 1);
                    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Ứng Lương");
                    long totalAdvance = 0;
                    foreach (EmployeeAdvanceDTO advance in _listAdvance)
                    {
                        totalAdvance += advance.TotalAdvance;
                    }
                    employeeSalary.TotalAdvanceSalary = totalAdvance;
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    _empBus.UpdateEmployeeSalary(employeeSalary);
                    
                }
            }


        }

        private void repositoryItemButtonEdit3_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var view = sender as ColumnView;
            var employeeSalary = gridView1.GetRow(rowIndex) as EmployeeSalaryDTO;
           
            if (_isNew)
            {
                if (employeeSalary != null)
                {
                    string message;
                    int pos;

                    if (!ValidateEmployeeSalaryBeforeCLick(employeeSalary, out message, out pos))
                    {

                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                    {
                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                     
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    else
                    {
                        using (var advanceManagementForm = new AdvanceManagement("Thu Nợ Hộ", employeeSalary))
                        {
                            advanceManagementForm.ShowDialog();
                            employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                            var year = employeeSalary.MonthFormat.Year;
                            var month = employeeSalary.MonthFormat.Month;
                            _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                            _fromDate = new DateTime(year, month, 1);
                            _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Thu Nợ Hộ");
                            long totalAdvance = 0;
                            foreach (EmployeeAdvanceDTO advance in _listAdvance)
                            {
                                totalAdvance += advance.TotalAdvance;
                            }
                            employeeSalary.Debt1 = totalAdvance;
                            employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                            _empBus.UpdateEmployeeSalary(employeeSalary);
                          
                        }

                    }
                }
            }
            else 
            {
                using (var advanceManagementForm = new AdvanceManagement("Thu Nợ Hộ", employeeSalary))
                {
                    advanceManagementForm.ShowDialog();

                    employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                    var year = employeeSalary.MonthFormat.Year;
                    var month = employeeSalary.MonthFormat.Month;
                    _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                    _fromDate = new DateTime(year, month, 1);
                    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Thu Nợ Hộ");
                    long totalAdvance = 0;
                    foreach (EmployeeAdvanceDTO advance in _listAdvance)
                    {
                        totalAdvance += advance.TotalAdvance;
                    }
                    employeeSalary.Debt1 = totalAdvance;
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    _empBus.UpdateEmployeeSalary(employeeSalary);
                  
                }
            }
        }

        private void repositoryItemButtonEdit4_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            int rowIndex = gridView1.FocusedRowHandle;
            var view = sender as ColumnView;
            var employeeSalary = gridView1.GetRow(rowIndex) as EmployeeSalaryDTO;
            if (_isNew)
            {
                if (employeeSalary != null)
                {
                    string message;
                    int pos;

                    if (!ValidateEmployeeSalaryBeforeCLick(employeeSalary, out message, out pos))
                    {

                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    if (!ValidateEmployeeSalary(employeeSalary, out message, out pos))
                    {
                        if (view != null)
                        {
                            view.ClearColumnErrors();
                            view.SetColumnError(view.Columns[pos], message, ErrorType.Warning);
                        }
                 
                        DialogResult result = MessageBox.Show(message + "\nBạn có muốn nhập lại thông tin", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.No)
                        {
                            _isNew = false;

                            LoadData();
                        }
                    }
                    else
                    {
                        using (var advanceManagementForm = new AdvanceManagement("Đền Vật Tư", employeeSalary))
                        {
                            advanceManagementForm.ShowDialog();
                            employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                            var year = employeeSalary.MonthFormat.Year;
                            var month = employeeSalary.MonthFormat.Month;
                            _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                            _fromDate = new DateTime(year, month, 1);
                            _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Đền Vật Tư");
                            long totalAdvance = 0;
                            foreach (EmployeeAdvanceDTO advance in _listAdvance)
                            {
                                totalAdvance += advance.TotalAdvance;
                            }
                            employeeSalary.Debt2 = totalAdvance;
                            employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                            _empBus.UpdateEmployeeSalary(employeeSalary);
                          
                        }

                    }
                }
            }
            else
            {
                using (var advanceManagementForm = new AdvanceManagement("Đền Vật Tư", employeeSalary))
                {
                    advanceManagementForm.ShowDialog();
                    employeeSalary.MonthFormat = DateTime.ParseExact(employeeSalary.Month + "01 00:00:00", "yyyyMMdd hh:mm:ss", null);
                    var year = employeeSalary.MonthFormat.Year;
                    var month = employeeSalary.MonthFormat.Month;
                    _toDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                    _fromDate = new DateTime(year, month, 1);
                    _listAdvance = _empBus.LoadAdvanceByEmployeeID(employeeSalary.EmployeeID, _fromDate, _toDate, "Đền Vật Tư");
                    long totalAdvance = 0;
                    foreach (EmployeeAdvanceDTO advance in _listAdvance)
                    {
                        totalAdvance += advance.TotalAdvance;
                    }
                    employeeSalary.Debt2 = totalAdvance;
                    employeeSalary = ConvertValueForEmployeeSalary(employeeSalary);
                    _empBus.UpdateEmployeeSalary(employeeSalary);
                   
                }
            }
            
            
        }

        private void EmployeeExpenseReceipts_Load(object sender, EventArgs e)
        {
            _isEdit = false;
            LoadData();
        }

        private void btnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.ReadOnly = false;
        }

     


  

   
    }
}